import tkinter as tk
import threading
import datetime
from tkinter import messagebox

def enter():
    global entry_username
    global entry_password
    root = tk.Tk()
    root.title("登陆注册页面")
    root.geometry("300x200+700+300")

# 创建用户名和密码输入框
    label_username = tk.Label(root, text="用户名：")
    label_username.pack()
    entry_username = tk.Entry(root)
    entry_username.pack()

    label_password = tk.Label(root, text="密码：")
    label_password.pack()
    entry_password = tk.Entry(root, show="*")
    entry_password.pack()

# 创建登录和注册按钮
    button_enter = tk.Button(root, text="登录", command=root.destroy)
    button_enter.pack(pady=5)

    button_register = tk.Button(root, text="注册", command=register)
    button_register.pack(pady=5)

    root.mainloop()
def login():

    username = entry_username.get()
    password = entry_password.get()

    if username == "张" and password == "123":
        messagebox.showinfo("登录成功", "欢迎进入待办事项，" + username + "!")
    else:
        messagebox.showerror("登录失败", "用户名或密码错误！")


def register():
    username = entry_username.get()
    password = entry_password.get()

    if username and password:
        messagebox.showinfo("注册成功", "注册成功，用户名：" + username + "，密码：" + password)
    else:
        messagebox.showwarning("注册失败", "请输入用户名和密码！")
enter()
class TodoItem:
    def __init__(self, title, description, deadline, priority):
        self.title = title
        self.description = description
        self.deadline = deadline
        self.priority = priority
        self.completed = False

    def __str__(self):
        status = "完成" if self.completed else "未完成"
        return f"标题: {self.title}\n描述: {self.description}\n截止日期: {self.deadline}\n优先级: {self.priority}\n状态: {status}"

class TodoManagerApp:
    def __init__(self, root):
        self.root = root
        self.root.title("待办事项管理系统")

        self.todo_list = []
        self.current_index = -1

        self.setup_ui()

    def setup_ui(self):
        # 标签和输入框
        tk.Label(self.root, text="标题:").grid(row=0, column=0, padx=5, pady=5)
        self.title_entry = tk.Entry(self.root)
        self.title_entry.grid(row=0, column=1, padx=5, pady=5)

        tk.Label(self.root, text="描述:").grid(row=1, column=0, padx=5, pady=5)
        self.description_entry = tk.Entry(self.root)
        self.description_entry.grid(row=1, column=1, padx=5, pady=5)

        tk.Label(self.root, text="截止日期 (YYYY-MM-DD):").grid(row=2, column=0, padx=5, pady=5)
        self.deadline_entry = tk.Entry(self.root)
        self.deadline_entry.grid(row=2, column=1, padx=5, pady=5)

        tk.Label(self.root, text="优先级:").grid(row=3, column=0, padx=5, pady=5)
        self.priority_entry = tk.Entry(self.root)
        self.priority_entry.grid(row=3, column=1, padx=5, pady=5)

        # 按钮
        tk.Button(self.root, text="添加任务", command=self.add_task).grid(row=4, column=0, columnspan=1,padx=5,
                                                                          pady=10)
        tk.Button(self.root, text="删除任务", command=self.delete_task).grid(row=5, column=0, columnspan=1, padx=5,
                                                                             pady=10)
        tk.Button(self.root, text="查看所有任务", command=self.show_all_tasks).grid(row=4, column=1, columnspan=1,
                                                                                    padx=5, pady=10)
        tk.Button(self.root, text="编辑任务", command=self.edit_task).grid(row=6, column=0, columnspan=1, padx=5,
                                                                           pady=10)
        tk.Button(self.root, text="标记任务为完成", command=self.mark_completed).grid(row=5, column=1, columnspan=1,
                                                                                      padx=5, pady=10)
        tk.Button(self.root, text="查看已完成任务", command=self.show_completed_tasks).grid(row=6, column=1,
                                                                                            columnspan=1, padx=5,
                                                                                            pady=10)
        tk.Button(self.root, text="提醒", command=self.set_reminder).grid(row=7, column=1,
                                                                                            columnspan=1, padx=5,
                                                                                            pady=10)
        # 显示区域
        self.display_text = tk.Text(self.root, width=60, height=15)
        self.display_text.grid(row=10, column=0, columnspan=2, padx=5, pady=10)

    def add_task(self):
        title = self.title_entry.get()
        description = self.description_entry.get()
        deadline = self.deadline_entry.get()
        priority = self.priority_entry.get()

        if title and description and deadline and priority:
            item = TodoItem(title, description, deadline, priority)
            self.todo_list.append(item)
            messagebox.showinfo("成功", "任务添加成功!")
            self.clear_entries()
        else:
            messagebox.showerror("错误", "请填写所有字段!")

    def show_all_tasks(self):
        self.display_text.delete(1.0, tk.END)
        for index, item in enumerate(self.todo_list):
            self.display_text.insert(tk.END, f"任务编号: {index}\n")
            self.display_text.insert(tk.END, str(item) + "\n")
            self.display_text.insert(tk.END, "--------------------\n")

    def delete_task(self):
        index = self.get_current_index()
        if index != -1:
            del self.todo_list[index]
            messagebox.showinfo("成功", "任务删除成功!")
            self.clear_entries()
            self.show_all_tasks()

    def edit_task(self):
        index = self.get_current_index()
        if index != -1:
            title = self.title_entry.get()
            description = self.description_entry.get()
            deadline = self.deadline_entry.get()
            priority = self.priority_entry.get()
            if title and description and deadline and priority:
                self.todo_list[index].title = title
                self.todo_list[index].description = description
                self.todo_list[index].deadline = deadline
                self.todo_list[index].priority = priority
                messagebox.showinfo("成功", "任务编辑成功!")
                self.clear_entries()
                self.show_all_tasks()
            else:
                messagebox.showerror("错误", "请填写所有字段!")

    def delete_task(self):
        index = self.get_current_index()
        if index != -1:
            del self.todo_list[index]
            messagebox.showinfo("成功", "任务删除成功!")
            self.clear_entries()
            self.show_all_tasks()
    def mark_completed(self):
        index = self.get_current_index()
        if index != -1:
            self.todo_list[index].completed = True
            messagebox.showinfo("成功", "任务标记为完成!")
            self.show_all_tasks()

    def mark_completed(self):
        index = self.get_current_index()
        if index != -1:
            self.todo_list[index].completed = True
            messagebox.showinfo("成功", "任务标记为完成!")
            self.show_all_tasks()

    def set_reminder(self):
        reminder_window = tk.Toplevel()
        reminder_window.title("设置提醒")

        def show_reminder():
            message = reminder_entry.get()
            reminder_time = reminder_time_entry.get()
            if message and reminder_time:
                try:
                    # 解析用户输入的时间
                    reminder_datetime = datetime.datetime.strptime(reminder_time, "%Y-%m-%d %H:%M:%S")
                    # 计算当前时间和提醒时间的时间差
                    delta = (reminder_datetime - datetime.datetime.now()).total_seconds()
                    if delta > 0:
                        # 启动定时器，到时提醒
                        reminder_timer = threading.Timer(delta, lambda: messagebox.showinfo("提醒", message))
                        reminder_timer.start()
                        messagebox.showinfo("设置成功", f"提醒将在 {reminder_time} 提醒。")
                        reminder_window.destroy()
                    else:
                        messagebox.showerror("错误", "提醒时间必须是未来的时间。")
                except ValueError:
                    messagebox.showerror("错误", "请输入正确的时间格式：YYYY-MM-DD HH:MM:SS")

        reminder_label = tk.Label(reminder_window, text="输入提醒消息:")
        reminder_label.pack(pady=10)

        reminder_entry = tk.Entry(reminder_window, width=30)
        reminder_entry.pack(pady=10)

        reminder_time_label = tk.Label(reminder_window, text="输入提醒时间（YYYY-MM-DD HH:MM:SS）:")
        reminder_time_label.pack(pady=10)

        reminder_time_entry = tk.Entry(reminder_window, width=30)
        reminder_time_entry.pack(pady=10)

        remind_button = tk.Button(reminder_window, text="设置提醒", command=show_reminder)
        remind_button.pack(pady=10)

    def show_completed_tasks(self):
        self.display_text.delete(1.0, tk.END)
        completed_tasks = [task for task in self.todo_list if task.completed]
        for item in completed_tasks:
            self.display_text.insert(tk.END, str(item) + "\n")
            self.display_text.insert(tk.END, "--------------------\n")

    def get_current_index(self):
        try:
            index = int(self.title_entry.get())
            if 0 <= index < len(self.todo_list):
                self.current_index = index
                return index
            else:
                messagebox.showerror("错误", "无效的任务编号!")
                return -1
        except ValueError:
            messagebox.showerror("错误", "请输入有效的任务编号!")
            return -1

    def clear_entries(self):
        self.title_entry.delete(0, tk.END)
        self.description_entry.delete(0, tk.END)
        self.deadline_entry.delete(0, tk.END)
        self.priority_entry.delete(0, tk.END)


def main():
    root = tk.Tk()
    app = TodoManagerApp(root)
    root.mainloop()

if __name__ == "__main__":
    main()